/*  
 * @(#) DemoAjaxUserServiceImpl.java Create on 2012-5-11 下午12:27:28   
 *   
 * Copyright 2012 by xl.   
 */

package cn.com.demo.ajax.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import cn.com.common.base.bean.BackstageResult;
import cn.com.common.base.bean.PageBean;
import cn.com.demo.bean.DemoUserBean;
import cn.com.demo.dao.DemoUserDao;
import cn.tools.jackjson.JackJson;

/**
 * 
 * @author zhanghongliang
 * @date 2012-5-11
 */
@Service
public class DemoAjaxUserServiceImpl implements DemoAjaxUserService {

	@Resource
	DemoUserDao dao;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String query(String query) throws Exception {
		if (StringUtils.isBlank(query)) {
			return JackJson.fromObjectToJson(new BackstageResult(false, "请求参数错误"));
		}
		Map queryMap = JackJson.fromJsonToObject(query, HashMap.class);
		queryMap.put("_tableName", "test.user_info");
		PageBean<DemoUserBean> pageBean = this.dao.getPageBean(queryMap, DemoUserBean.class);

		return JackJson.fromObjectToJson(new BackstageResult(true, pageBean));
	}

	public String find(DemoUserBean oprBean) throws Exception {
		DemoUserBean resultBean = null;
		if (oprBean.getId() != null && oprBean.getId() > 0) {
			resultBean = this.dao.get(oprBean.getId(), DemoUserBean.class);
		}

		if (resultBean == null) {
			resultBean = oprBean;
			oprBean.setId(-1);
			oprBean.setSex(1);
		}
		return JackJson.fromObjectToJson(new BackstageResult(true, resultBean));
	}

	public String save(DemoUserBean oprBean) throws Exception {
		int oprCount = 0;
		if (oprBean.getId() == null || oprBean.getId() < 0) {
			oprCount = (int) this.dao.insert(oprBean, false);
		} else {
			oprCount = this.dao.updateNotNull(oprBean);
		}

		return JackJson.fromObjectToJson(new BackstageResult(true, oprCount));
	}

	public String delete(DemoUserBean oprBean) throws Exception {
		int oprCount = this.dao.delete(oprBean);
		return JackJson.fromObjectToJson(new BackstageResult(true, oprCount));
	}
}
